/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.Servlets;
import com.thinkgem.jeesite.modules.bus.dao.VerificationCodeDao;
import com.thinkgem.jeesite.modules.bus.entity.VerificationCode;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 验证码信息Service
 * @author ZYJ
 * @version 2019-05-31
 */
@Service
@Transactional(readOnly = true)
public class VerificationCodeService extends CrudService<VerificationCodeDao, VerificationCode> {
	@Autowired
	private VerificationCodeDao verificationCodeDao;

	public VerificationCode get(String id) {
		return super.get(id);
	}
	
	public List<VerificationCode> findList(VerificationCode verificationCode) {
		return super.findList(verificationCode);
	}
	
	public Page<VerificationCode> findPage(Page<VerificationCode> page, VerificationCode verificationCode) {
		return super.findPage(page, verificationCode);
	}
	
	@Transactional(readOnly = false)
	public void save(VerificationCode verificationCode) {
		super.save(verificationCode);
	}
	
	@Transactional(readOnly = false)
	public void delete(VerificationCode verificationCode) {
		super.delete(verificationCode);
	}
	
	/**
	 * 
	* @Title: saveCode 
	* @author: ZYJ
	* @date: 2019年5月31日 下午2:54:43 
	* @Description: 根据电话号判断此号码是否存在记录，如果不存在就新增一条，存在就修改验证码和过期时间
	* @param:  code
	* @param: mobile
	* @return: void
	* @throws
	 */
	
	@Transactional(readOnly = false)
	public String saveCode_bak(String code,String mobile) {
		//根据电话号判断此号码是否存在未使用记录，如果不存在就新增一条，存在且没过期更新过期时间，存在且过期了修改是否已用为已使用同时新增一条
		List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(mobile);
		VerificationCode verificationCode = new VerificationCode();
		verificationCode.setMobile(mobile);
		verificationCode.setCode(code);
		long currentTime = System.currentTimeMillis() + 10 * 60 * 1000;
		Date deadline = new Date(currentTime);//新的过期时间
		verificationCode.setDeadline(deadline);
		//限制同一个ip地址能获取几次验证码
		String ip = StringUtils.getRemoteAddr(Servlets.getRequest());
		verificationCode.setIp(ip);
		if(verificationCodes.size() == 0){
			super.save(verificationCode);
			return code;
		}else{
			VerificationCode vc = getVerificationCode(verificationCodes);
			Date oldDeadline = vc.getDeadline();//原过期时间
			Date nowDate = new Date();
			if(nowDate.after(oldDeadline)){
				vc.setUsedStatus(1);
				vc.preUpdate();
				if(StringUtils.isBlank(UserUtils.getUser().getId())){
					User user = new User();
					user.setId(mobile);
					vc.setUpdateBy(user);
				}
				dao.update(vc);
				super.save(verificationCode);
				return code;
			}else{
				vc.setDeadline(deadline);
				vc.preUpdate();
				if(StringUtils.isBlank(UserUtils.getUser().getId())){
					User user = new User();
					user.setId(mobile);
					vc.setUpdateBy(user);
				}
				dao.update(vc);
				return vc.getCode();
			}
		}
	}
	/**
	 * @author Zhang Xingliang
	 * @description 优化发送短信验证码
	 * @date 9:17 2019/7/29 0029
	 * @param code, mobile
	 * @return java.lang.String
	 **/
	@Transactional(readOnly = false)
	public String saveCode(String code,String mobile) {
		//根据电话号判断此号码是否存在未使用记录，如果不存在就新增一条，存在且没过期更新过期时间，存在且过期了修改是否已用为已使用同时新增一条
		List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(mobile);
		//限制同一个ip地址能获取几次验证码
		if(verificationCodes.size() == 0){
			super.save(new VerificationCode(code,mobile,StringUtils.getRemoteAddr(Servlets.getRequest())
					,new Date(System.currentTimeMillis() + 10 * 60 * 1000)));
		}else{
			VerificationCode vc = getVerificationCode(verificationCodes);
			if(new Date().after(vc.getDeadline())){
				vc.setUsedStatus(1);
				super.save(new VerificationCode(code,mobile,StringUtils.getRemoteAddr(Servlets.getRequest())
						,new Date(System.currentTimeMillis() + 10 * 60 * 1000)));
			}else{
				vc.setDeadline(new Date(System.currentTimeMillis() + 10 * 60 * 1000));
				code = vc.getCode();
			}
			vc.preUpdate();
			if(StringUtils.isBlank(UserUtils.getUser().getId())){
				User user = new User();
				user.setId(mobile);
				vc.setUpdateBy(user);
			}
			dao.update(vc);
		}
		return code;
	}
	
	/**
	 * 
	* @Title: verfMinute 
	* @author: ZYJ
	* @date: 2019年6月14日 下午5:06:29 
	* @Description: 限制一分钟内获取多次验证码
	* @param: mobile
	* @return: Boolean
	* @throws
	 */
	@Transactional(readOnly = false)
	public Boolean verfMinute(String mobile){
		List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(mobile);
		if(verificationCodes.size() == 0){
			return false;
		}else{
			VerificationCode vc = getVerificationCode(verificationCodes);
			long currentTime = System.currentTimeMillis();//当前时间
			long updateDate = vc.getUpdateDate().getTime() + 1 * 60 * 1000;//上一次生成验证码的时间加一分钟
			if(currentTime > updateDate){//两次获取验证码时间差超过一分钟
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 
	* @Title: getVerificationCode 
	* @author: ZYJ
	* @date: 2019年7月1日 下午4:24:00 
	* @Description: 获取一条有效验证码
	* @param:  verificationCodes
	* @return: VerificationCode
	* @throws
	 */
	@Transactional(readOnly = false)
	public VerificationCode getVerificationCode(List<VerificationCode> verificationCodes){
//		VerificationCode verificationCode;
//		if(verificationCodes.size() > 1){
			//进入此判断，说明获取的有效记录是 多条，取最后一条，删除其余多余记录，保证未使用的验证码唯一
//			verificationCode = verificationCodes.get(verificationCodes.size()-1);
			for(int i=0;i<verificationCodes.size()-1;i++){
//				VerificationCode v = verificationCodes.get(i);
				dao.delete(verificationCodes.get(i));
			}
//		}else{
//			verificationCode = verificationCodes.get(0);
//		}
		return verificationCodes.get(verificationCodes.size()-1);
	}
}